package com.movitech.mobile.cz.base.sys.controller;


import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.sys.entity.ext.AccountRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SysUserRequestVO;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.entity.SysUser;
import com.movitech.mobile.cz.base.sys.service.SysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

/**
 * <p>
 * SysUser 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sysUser")
@Api(value = "基础操作接口", description = "基础操作接口")
public class SysUserController {
    @Autowired
    @Qualifier("sysUserService")
    private SysUserService sysUserService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有接口",
            notes = "获取所有接口",
            response = SysUser.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SysUser> page = sysUserService.selectPage(new Page<SysUser>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "列表查询接口",
            notes = "列表查询接口",
            response = SysUser.class
    )
    public RestReturnResult searchList(@RequestBody SysUser sysUser,
                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SysUser> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sysUser);
        page = sysUserService.selectPage(page,wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSysUser/{id}")
    @ApiOperation(
            value = "对象ID获取接口",
            notes = "对对象ID获取接口",
            response = SysUser.class
    )
    public RestReturnResult addSysUser(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SysUser sysUser = sysUserService.selectById(id);
        restReturnResult.setData(sysUser);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "添加接口",
            notes = "添加接口",
            response = SysUser.class
    )
    public RestReturnResult add(@RequestBody SysUser sysUser) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sysUserService.insert(sysUser);
        restReturnResult.setData(sysUser);
        return restReturnResult;
    }

    @PostMapping("/batchAdd")
    @ApiOperation(
            value = "批量保存接口",
            notes = "批量保存接口",
            response = SysUser.class
    )
    public RestReturnResult batchAdd(@RequestBody List<SysUserRequestVO> sysUserRequestVOList) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        try {
            restReturnResult=sysUserService.batchAdd(sysUserRequestVOList);
        }catch (RuntimeException e){
            e.printStackTrace();
            restReturnResult= new RestReturnResult(CommonConstants.FAIL,e.getMessage());
        }
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "更新接口",
            notes = "更新接口",
            response = SysUser.class
    )
    public RestReturnResult update(@RequestBody SysUser sysUser) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sysUserService.updateById(sysUser);
        restReturnResult.setData(sysUser);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "通过ID删除接口",
            notes = "通过ID删除接口",
            response = SysUser.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sysUserService.deleteById(id);
        return restReturnResult;
    }

    @PostMapping("/updateAccountStatus")
    @ApiOperation(value = "修改账户状态接口", notes = "修改账户状态接口")
    public RestReturnResult updateAccountStatus(@RequestBody AccountRequestVO accountRequestVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        try {
            SysUser user=sysUserService.selectById(accountRequestVO.getId());
            if(user==null){
                restReturnResult=new RestReturnResult(CommonConstants.FAIL,"用户不存在");
            }else{
                user.setLoginFlag(accountRequestVO.getLoginFlag());
                boolean updateFlag = sysUserService.updateById(user);
                if (!updateFlag) {
                    restReturnResult = RestReturnResult.error();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            restReturnResult = RestReturnResult.error();
        }
        return restReturnResult;
    }

    @GetMapping("/resetPassword/{id}")
    @ApiOperation(value = "重置密码接口", notes = "重置密码接口")
    public RestReturnResult resetPassword(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        try {
            SysUser user=sysUserService.selectById(id);
            if(user==null){
                restReturnResult=new RestReturnResult(CommonConstants.FAIL,"用户不存在");
            }else{
                user.setPassword(DigestUtils.md5Hex("abc123456"));
                boolean updateFlag = sysUserService.updateById(user);
                if (!updateFlag) {
                    restReturnResult = RestReturnResult.error();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            restReturnResult = RestReturnResult.error();
        }
        return restReturnResult;
    }


}

